package com.southminority.ethnic.controller.admin.course;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.controller.admin.vo.AdminPageReqVo;
import com.southminority.ethnic.controller.admin.vo.CourseTeacherCreateReqVo;
import com.southminority.ethnic.controller.admin.vo.CourseTeacherUpdateReqVo;
import com.southminority.ethnic.pojo.CourseTeacher;
import com.southminority.ethnic.service.course.CourseTeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

import java.util.List;

/**
 * 课程讲师后台管理Controller
 * 
 * @author SouthMinority
 */
@Slf4j
@Tag(name = "课程讲师管理后台", description = "管理员课程讲师管理接口")
@RestController
@RequestMapping("/admin/course/teacher")
@CrossOrigin
@Validated
public class CourseTeacherAdminController {

    @Autowired
    private CourseTeacherService courseTeacherService;

    /**
     * 分页查询讲师列表
     */
    @Operation(summary = "分页查询讲师列表")
    @PostMapping("/page")
    public R<Page<CourseTeacher>> getTeacherPage(@Valid @RequestBody AdminPageReqVo reqVo) {
        try {
            Page<CourseTeacher> page = new Page<>(reqVo.getPageNum(), reqVo.getPageSize());
            
            // 构建查询条件
            Page<CourseTeacher> result = courseTeacherService.getTeacherPageWithKeyword(page, reqVo.getKeyword());
            
            log.info("管理员分页查询讲师列表成功，页数: {}, 页面大小: {}, 关键词: {}", 
                    reqVo.getPageNum(), reqVo.getPageSize(), reqVo.getKeyword());
            return R.ok(result);
            
        } catch (Exception e) {
            log.error("管理员分页查询讲师列表失败", e);
            return R.fail("查询失败：" + e.getMessage());
        }
    }

    @Operation(summary = "查询所有讲师")
    @PostMapping("/list")
    public R<List<CourseTeacher>> getAllTeachers() {
        try {
            return R.ok(courseTeacherService.list());
        } catch (Exception e) {
            log.error("管理员查询所有讲师列表失败", e);
            return R.fail("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取讲师详情
     */
    @Operation(summary = "根据ID获取讲师详情")
    @PostMapping("/detail/{id}")
    public R<CourseTeacher> getTeacherDetail(@PathVariable Long id) {
        try {
            CourseTeacher teacher = courseTeacherService.getById(id);
            if (teacher == null) {
                return R.fail("讲师不存在");
            }
            
            log.info("管理员获取讲师详情成功，讲师ID: {}", id);
            return R.ok(teacher);
            
        } catch (Exception e) {
            log.error("管理员获取讲师详情失败，讲师ID: {}", id, e);
            return R.fail("获取详情失败：" + e.getMessage());
        }
    }

    /**
     * 创建讲师
     */
    @Operation(summary = "创建讲师")
    @PostMapping("/create")
    public synchronized R<CourseTeacher> createTeacher(@Valid @RequestBody CourseTeacherCreateReqVo reqVo) {
        try {
            CourseTeacher teacher = new CourseTeacher();
            BeanUtils.copyProperties(reqVo, teacher);
            
            boolean saved = courseTeacherService.save(teacher);
            if (saved) {
                log.info("管理员创建讲师成功，讲师名称: {}", teacher.getTeacherName());
                return R.ok(teacher);
            } else {
                return R.fail("创建失败");
            }
            
        } catch (Exception e) {
            log.error("管理员创建讲师失败", e);
            return R.fail("创建失败：" + e.getMessage());
        }
    }

    /**
     * 更新讲师
     */
    @Operation(summary = "更新讲师")
    @PostMapping("/update")
    public synchronized R<CourseTeacher> updateTeacher(@Valid @RequestBody CourseTeacherUpdateReqVo reqVo) {
        try {
            CourseTeacher teacher = new CourseTeacher();
            BeanUtils.copyProperties(reqVo, teacher);
            
            boolean updated = courseTeacherService.updateById(teacher);
            if (updated) {
                log.info("管理员更新讲师成功，讲师ID: {}, 讲师名称: {}", teacher.getId(), teacher.getTeacherName());
                return R.ok(teacher);
            } else {
                return R.fail("更新失败");
            }
            
        } catch (Exception e) {
            log.error("管理员更新讲师失败，讲师ID: {}", reqVo.getId(), e);
            return R.fail("更新失败：" + e.getMessage());
        }
    }

    /**
     * 删除讲师
     */
    @Operation(summary = "删除讲师")
    @PostMapping("/delete/{id}")
    public R<Boolean> deleteTeacher(@PathVariable Long id) {
        try {
            boolean deleted = courseTeacherService.removeById(id);
            if (deleted) {
                log.info("管理员删除讲师成功，讲师ID: {}", id);
                return R.ok(true);
            } else {
                return R.fail("删除失败");
            }
            
        } catch (Exception e) {
            log.error("管理员删除讲师失败，讲师ID: {}", id, e);
            return R.fail("删除失败：" + e.getMessage());
        }
    }
} 